﻿using CustomCmdPaster;
using System;
using System.IO;
using System.Text;

namespace LinyeeFileRead
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("欢迎使用LinyeeFileRead!");
            //初始化
            var cmd = new LinyeeCmd(opts=> {
                opts.Cmds = new LinyeeCmdOptionsItem[] {
                    new LinyeeCmdOptionsItem() {
                        CmdName ="Open" ,Params= new string[] { "file", "new" ,"f","n"} },
                    new LinyeeCmdOptionsItem() {
                        CmdName = "Read",Params= new string[] { "line", "l", "pos", "p" }  },
                    new LinyeeCmdOptionsItem() {
                        CmdName = "Write",Params= new string[] { "append", "a","over", "o" }  },
                    new LinyeeCmdOptionsItem() {
                        CmdName = "Close",Params= new string[] {  }  },
                    new LinyeeCmdOptionsItem() {
                        CmdName = "Echo",Params= new string[] {  }  },
                    new LinyeeCmdOptionsItem() {
                        CmdName = "Help",Params= new string[] {  }  },
                };
            });

            // open -f "E:\_Linyee\_语料库\_hasbak\7lore 乱码\7Lore_triple.csv"
            // read -l 12

            FileStream fs = null;
            StreamReader sr = null;
            bool readanykey = false;
            string exitmsg = null;
            bool echo = true;
            while (true)
            {
                Console.Write("LinyeeCII>");//Command Interactive Interface
                var cmdline = Console.ReadLine();
                if(echo) Console.WriteLine($"{cmdline}");

                if (cmdline == null)
                {
                    //文件管道输入时
                    exitmsg = "程序退出";
                    break;
                }
                if (string.IsNullOrEmpty(cmdline)) continue;
                if (cmdline.Equals("exit", StringComparison.OrdinalIgnoreCase)) break;

                LinyeeCmdItem cmditem = null;
                try
                {
                    cmditem=cmd.Parse(cmdline);
                }
                catch (Exception ex)
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.Write("Err:");
                    Console.WriteLine(ex.Message);
                    Console.ResetColor();
                    continue;
                }
                var cmdl = cmditem.CmdLower;
                //Console.WriteLine($"{Newtonsoft.Json.JsonConvert.SerializeObject(cmditem)}");
                switch (cmdl)
                {
                    case "open":
                        if (!cmditem.Paras.ContainsKey("f"))
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("Err:");
                            Console.WriteLine("未找定要打开的文件名");
                            Console.ResetColor();
                            break;
                        }
                        else
                        {
                            var f = cmditem.Paras["f"] ?? cmditem.Paras["file"];
                            fs = File.Open(f, FileMode.OpenOrCreate);
                            Console.WriteLine($"已打开文件({f})");
                            break;
                        }
                    case "read":
                        if (fs==null)
                        {
                            Console.ForegroundColor = ConsoleColor.Red;
                            Console.Write("Err:");
                            Console.WriteLine("请先执行 open -f <文件名>");
                            Console.ResetColor();
                            break;
                        }
                        if(sr==null) sr = new StreamReader(fs);
                        int lines = 0;
                        string res = "";
                        if (cmditem.Paras.ContainsKey("l") || cmditem.Paras.ContainsKey("lines"))
                        {
                            var ib= int.TryParse(cmditem.Paras["l"] ?? cmditem.Paras["lines"],out lines);
                        }

                        if (lines > 0)
                        {
                            StringBuilder lbd = new StringBuilder();
                            for (var fi = 0; fi < lines; fi++)
                            {
                                if (sr.EndOfStream) break;
                                lbd.AppendLine(sr.ReadLine());
                            }
                            res = lbd.ToString();
                        }
                        else
                        {
                            res = sr.ReadLine();
                        }
                        Console.WriteLine(res);
                        break;
                    case "write":
                        break;
                    case "close":
                        fs = null;
                        Console.WriteLine("文件已关闭");
                        break;
                    case "echo":
                        if(cmditem.Paras.Count==0)echo=true ;
                        else if (cmditem.Paras.ContainsKey("0001"))
                        {
                            var val = cmditem.Paras["0001"];
                            if(val.Equals("on",StringComparison.OrdinalIgnoreCase)) echo = true;
                            else if (val.Equals("off", StringComparison.OrdinalIgnoreCase)) echo = false;
                            else
                            {
                                Console.ForegroundColor = ConsoleColor.Red;
                                Console.Write("Err:");
                                Console.WriteLine("Echo 只能接受 空值 on off");
                                Console.ResetColor();
                            }
                        }
                        break;
                    case "help":
                        Console.WriteLine(cmd.Help);
                        break;
                    default:
                        Console.WriteLine($"{Newtonsoft.Json.JsonConvert.SerializeObject(cmditem)}");
                        break;
                }
            }

            if( !string.IsNullOrEmpty(exitmsg))Console.WriteLine($"{exitmsg}");

            if (readanykey)
            {
                Console.ReadKey();
            }
        }
    }
}
